<template>
  <div>
    <el-dialog title="编辑员工" :visible.sync="visible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          prop="username"
          label="姓名:"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.username"
            style="width:50%"
            placeholder="1-30个字符"
          />
        </el-form-item>
        <el-form-item prop="mobile" label="手机:" :label-width="formLabelWidth">
          <el-input
            v-model="form.mobile"
            style="width:50%"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item
          prop="timeOfEntry"
          label="入职时间:"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="form.timeOfEntry"
            style="width:50%"
            type="date"
            placeholder="选择日期"
          /></el-form-item>
        <el-form-item
          prop="formOfEmployment"
          label="聘用形式:"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="form.formOfEmployment"
            style="width:50%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :value="item.id"
              :label="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="workNumber"
          label="工号:"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.workNumber"
            style="width:50%"
            placeholder="1-20个字符"
          />
        </el-form-item>
        <el-form-item
          prop="departmentName"
          label="部门:"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.departmentName"
            style="width:50%"
            placeholder="请选择"
            @focus="getTreeData"
          />
          <el-tree
            v-if="showTree"
            class="dep-tree"
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
          />
        </el-form-item>
        <el-form-item
          prop="correctionTime"
          label="转正时间:"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="form.correctionTime"
            style="width:50%"
            type="date"
            placeholder="选择日期"
          /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="addEmp">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments } from '../../../api/departments'
import { addEmployees } from '../../../api/employees'
import { formatData } from '../../../utils/index'
import EmployeEnum from '../../../api/constant/employees'
export default {
  data() {
    return {
      rules: {
        username: [
          { required: true, message: '请输入姓名', targgle: 'blur' },
          { min: 1, max: 30, message: '请输入1-30个字符', targgle: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', targgle: 'blur' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式有误',
            targgle: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '请输入聘用形式', targgle: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请填写工号', targgle: 'blur' },
          { min: 1, max: 20, message: '请输入1-20个字符', targgle: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门信息', targgle: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请输入入职时间', targgle: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入转正时间', targgle: 'blur' }
        ]
      },
      // 显示Dialog
      visible: false,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      formLabelWidth: '120px',
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // tree数据
      treeData: [],
      // 显示tree
      showTree: false,
      hireType: EmployeEnum.hireType
    }
  },
  methods: {
    // 新增员工

    async addEmp() {
      this.$refs.form.validate(valid => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
      await addEmployees(this.form)
      this.visible = false
      this.$message.success('新增成功')
      this.$parent.getData()
      this.form = {}
    },
    // 点击部门
    async getTreeData() {
      this.showTree = true
      const res = await getDepartments()
      this.treeData = formatData(res.depts, '')
      //   console.log(treeData)
    },
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.showTree = false
    }
  }
}
</script>

<style>
.dep-tree {
  border: 1px solid #dcdfe6;
  width: 50%;
  position: absolute;
  z-index: 999;
  border-radius: 4px;
}
</style>
